package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.JobInfoMapper;
import com.example.mapper.JobMapper;
import com.example.pojo.Job;
import com.example.pojo.JobInfo;
import com.example.pojo.TakersJob;
import com.example.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job>implements JobService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private JobInfoMapper jobInfoMapper;

    @Override
    public List<Job> getExamineWorks() {
        List<Job> jobList = jobMapper.selectList(new LambdaQueryWrapper<Job>().eq(Job::getStatus,"1"));
        return jobList;
    }

    @Override
    public int rejectOne(String id,Integer userId,String msg) {
        int workId = Integer.parseInt(id);
        System.out.println("int_jobId"+workId);
        UpdateWrapper<Job> wrapper = new UpdateWrapper<>();
        int row = jobMapper.update(null, wrapper
                .set("status", "3")
                .eq("id", workId));
        JobInfo jobInfo = new JobInfo();
        jobInfo.setJobId(workId);
        jobInfo.setExamineUser(userId);
        jobInfo.setExamineOption("驳回");
        jobInfo.setExamineInfo(msg);
        jobInfo.setCreateTime(LocalDateTime.now());
        jobInfoMapper.insert(jobInfo);
        return row;
    }

    @Override
    public int accessOne(String id, Integer userId) {
        int workId = Integer.parseInt(id);
        jobMapper.update(null,new UpdateWrapper<Job>()
                .set("status","2")
                .set("process","1")
                .eq("id",workId));
        JobInfo jobInfo = new JobInfo();
        jobInfo.setJobId(workId);
        jobInfo.setExamineUser(userId);
        jobInfo.setExamineOption("同意");
        jobInfo.setExamineInfo("审核通过");
        jobInfo.setCreateTime(LocalDateTime.now());
        jobInfoMapper.insert(jobInfo);
        return 0;
    }

    @Override
    public int updateJobsToReject(List<String> idlist) {
        List<Integer> intIdList = idlist.stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        List<Job> jobList = new ArrayList<>();
        for (Integer id : intIdList){
            Job job = new Job();
            job.setId(id);
            job.setStatus("3");
            jobList.add(job);
        }
        updateBatchById(jobList);
        return 0;
    }

    @Override
    public int updateJobsToAccess(List<String> idList) {
        List<Integer> intIdList = idList.stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        List<Job> jobList = new ArrayList<>();
        for (Integer id : intIdList){
            Job job = new Job();
            job.setId(id);
            job.setStatus("2");
            job.setProcess("1");
            jobList.add(job);
        }
        updateBatchById(jobList);
        return 0;
    }

    @Override
    public List<Job> getCompleteJobs(List<Integer> idList) {
        // 获取当前日期
        LocalDateTime now = LocalDateTime.now();
        // 获取当前月份的开始时间和结束时间
        LocalDateTime startOfMonth = now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfMonth = now.withDayOfMonth(now.toLocalDate().lengthOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 将 LocalDateTime 转换为 Date
        Date startDate = Date.from(startOfMonth.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfMonth.atZone(ZoneId.systemDefault()).toInstant());

        // 查询满足条件的 Job
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idList)
                .ge("publish_time", startDate)
                .le("publish_time", endDate);

        List<Job> jobList = jobMapper.selectList(queryWrapper);
        return jobList;
    }


    @Override
    public List<Job> getStudentAllJobs(List<TakersJob> takersJobList) {


        List<Job> jobList = new ArrayList<>();
        List<Integer> idList = new ArrayList<>();
        for (TakersJob takersJob : takersJobList){
            idList.add(takersJob.getJobId());
        }

        if (idList.size() != 0){
            // 获取当前日期
            LocalDateTime now = LocalDateTime.now();
            // 获取当前月份的开始时间和结束时间
            LocalDateTime startOfMonth = now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endOfMonth = now.withDayOfMonth(now.toLocalDate().lengthOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(999999999);

            // 将 LocalDateTime 转换为 Date
            Date startDate = Date.from(startOfMonth.atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endOfMonth.atZone(ZoneId.systemDefault()).toInstant());

            // 查询满足条件的 Job
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", idList)
                    .ge("start_time", startDate)
                    .le("end_time", endDate);

            jobList = jobMapper.selectList(queryWrapper);
        }
        System.out.println("用户的兼职："+jobList);
        List<Job> newJobList = jobList.stream().map(job -> {
            for (TakersJob takersJob: takersJobList){
                if (job.getId() == takersJob.getJobId() && takersJob.getAccess() == 0){
                    job.setProcess("1");
                }
                if (job.getId() == takersJob.getJobId() && takersJob.getAccess() == 2){
                    job.setProcess("6");
                }
            }
            return job;
        }).collect(Collectors.toList());
        return newJobList;
    }

    @Override
    public List<Job> getAccessWorks() {
        List<Job> jobList = jobMapper.selectList(new LambdaQueryWrapper<Job>().eq(Job::getStatus,"2"));
        return jobList;
    }
    @Override
    public void updateJobToReceive(Integer jobId) {
        Job job = new Job();
        job.setId(jobId);  // 设置主键
        job.setProcess("2"); // 需要更新的字段

        int rows = jobMapper.updateById(job);
    }

    @Override
    public Boolean jobInWork(List<TakersJob> takersJobList) {

        List<Job> jobList = new ArrayList<>();
        List<Integer> idList = new ArrayList<>();
        for (TakersJob takersJob : takersJobList){
            if (takersJob.getAccess() == 1){
                idList.add(takersJob.getJobId());
            }
        }
        if (idList.size() != 0){
            // 查询满足条件的 Job
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", idList);
            jobList = jobMapper.selectList(queryWrapper);
        }

        if (jobList.size() != 0){
            jobList.stream().forEach(job -> {
                if (job.getStartTime().isBefore(LocalDateTime.now())){
                    Job updateJob = new Job();
                    updateJob.setId(job.getId());
                    updateJob.setProcess("3");
                    jobMapper.updateById(updateJob);
                }
            });
        }

        return true;
    }
}
